<?php
namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Lib\Response;
use App\Http\Model\Hint;
use App\Http\Services\RedPacketRainServices;

//许愿池
class ApiRedPacketRainController extends BaseController
{
    protected $redPacketRainService;

    public function __construct(){
        $this->redPacketRainService = new RedPacketRainServices();
    }

    /**
     * 红包雨获取即将开始的一场数据
     */
    public function redPacketRainGetNewly(Request $request)
    {
        $userId = $this->getLoginUserId();
        if(empty($userId)) {
            Response::returnResult(Hint::LOGIN_OUT);
        }
        $roomId = (int) $request->input("room_id");
        if(empty($roomId)) {
            Response::returnResult(Hint::PARAM_ERROR);
        }

        $data = $this->redPacketRainService->redPacketRainGetNewly($userId, $roomId);

        Response::returnResult($data['code'], $data['data'], $data['msg']);

    }

    /**
     * 红包雨抢红包
     */
    public function redPacketRainDraw(Request $request)
    {
        $userId = $this->getLoginUserId();
        if(empty($userId)) {
            Response::returnResult(Hint::LOGIN_OUT);
        }
        $roomId = (int) $request->input("room_id");
        $rainId = (int) $request->input("rain_id");
        if(empty($roomId) || empty($rainId)) {
            Response::returnResult(Hint::PARAM_ERROR);
        }
        //锁
        $lockKey = 'REDPACKETRAIN:redPacketRainDraw:'.$userId;
        if (!redis_lock($lockKey, 10)){
            Response::returnResult(Hint::SAME_TIME_NOT_REPEAT, [], '');
        }
        $data = $this->redPacketRainService->redPacketRainDraw($userId, $roomId, $rainId);
        redis_unlock($lockKey);
        Response::returnResult($data['code'], $data['data'], $data['msg']);
    }

}
